//
//  UIView+Badge.m
//  FDLive
//
//  Created by CoderLT on 2017/9/19.
//  Copyright © 2017年 FDLive. All rights reserved.
//

#import "UIView+Badge.h"

@implementation BadgeLabel
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    if (self = [super initWithCoder:aDecoder]) {
        [self setup];
    }
    return self;
}
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setup];
    }
    return self;
}
- (void)awakeFromNib {
    [super awakeFromNib];
    [self setup];
}
- (void)setup {
    self.backgroundColor = [UIColor colorWithRGB:0xff0000];
    self.clipsToBounds = YES;
    self.font = [UIFont systemFontOfSize:9.0f];
    self.textColor = [UIColor whiteColor];
    self.textAlignment = NSTextAlignmentCenter;
}
- (void)layoutSubviews {
    [super layoutSubviews];
    self.layer.cornerRadius = self.height/2;
}
- (CGSize)intrinsicContentSize {
    CGFloat badgeSize = self.badgeSize > 0 ? self.badgeSize : 12;
    if (self.text.length == 0) {
        return CGSizeMake(5, 5);
    }
    else if (self.text.length == 1 && self.text.integerValue > 0) {
        return CGSizeMake(badgeSize, badgeSize);
    }
    
    CGSize size = [super intrinsicContentSize];
    size.width += 8;
    size.height = badgeSize;
    return size;
}
@end

@implementation UIView (Badge)
- (void)showUnread:(NSString *)text {
    return [self showUnread:text offset:CGPointZero];
}
- (void)showUnread:(NSString *)text offset:(CGPoint)offset{
    return [self showUnread:text badgeSize:12 offset:offset];
}
- (void)showUnread:(NSString *)text badgeSize:(CGFloat)badgeSize offset:(CGPoint)offset {
    [self showUnread:text badgeSize:badgeSize completion:^(BadgeLabel *badge, BOOL firstShow) {
        if (firstShow) {
            badge.offset = offset;
            [badge mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerX.equalTo(badge.superview.mas_right).offset(offset.x);
                make.centerY.equalTo(badge.superview.mas_top).offset(offset.y);
            }];
        }
        else if (!CGPointEqualToPoint(offset, badge.offset)) {
            badge.offset = offset;
            [badge mas_updateConstraints:^(MASConstraintMaker *make) {
                make.centerX.equalTo(badge.superview.mas_right).offset(offset.x);
                make.centerY.equalTo(badge.superview.mas_top).offset(offset.y);
            }];
        }
    }];
}
- (void)showUnread:(NSString *)text badgeSize:(CGFloat)badgeSize completion:(void (^)(BadgeLabel *, BOOL))completion {
    const int tag = 23977199;
    if (text) {
        BadgeLabel *badge = [self viewWithTag:tag];
        BOOL firstShow = NO;
        if (!badge || ![badge isKindOfClass:[BadgeLabel class]]) {
            badge = [[BadgeLabel alloc] init];
            badge.tag = tag;
            [self addSubview:badge];
            firstShow = YES;
        }
        badge.badgeSize = badgeSize;
        badge.text = text;
        
        !completion ?: completion(badge, firstShow);
    }
    else{
        [[self viewWithTag:tag] removeFromSuperview];
    }
}
@end
